jQ(function(){
	//InitBackGroundPic();
	jQ("#pwdipt").keydown(function(event){
		if(event.keyCode==13){
			signIn();
		}
	});
	jQ("#emailipt").focus();
	jQ.ajax({
		type: "POST",
		url:"userAjax!checkExistUser.action",
		success:function(data){
			if(data!=null){
				var dataObj=eval("("+data+")");
				if(dataObj['loginflag'])
					window.location.href=jQ("#ctxVal").val()+"/myclip/calendar.html";
			}
		}
	});
});


function InitBackGroundPic(){
	var abc=parseInt(Math.random()*3+1);
	jQ("#about_reminder").empty().append("<img src='image/clip24-"+abc+".jpg' width='878'/>");
}
//Jquery的弹出框
function jalert(info,LFun){
	jQ("#ALERT p").text(info);
	if(LFun!=null)jQ("#ALERT").bind("LFun",LFun);
	jQ("#ALERT").dialog('open');
	jQ("#ALERT").dialog({buttons:{"确定":function(){
		jQ(this).dialog('close');
	}}});
}

function resendActiveEmail(){
	var username = jQ("#emailipt").val();
	var password = jQ("#pwdipt").val();
	jQ("#ALERT").dialog({
		buttons:[{
			text : "确定",
			click: function() {jQ(this).dialog("close");}
		}
		]
	});
	jQ.ajax({
		type: "POST",
		url: "userAjax!resendActiveEmail.action",
		data:{
			emailInput : username,
			pwdInput : password
		},
		success:function(data){
			if(data!=null){
				var dataObj=eval("("+data+")");
				if(dataObj.flag){
					jQ("#ALERT p").empty().append("激活邮件发送成功，请尽快登录邮箱激活。");
				}else{
					jQ("#ALERT p").empty().append("账号密码错误。");
				}
			}
		}
	});
}

function signIn(){
	var username = jQ("#emailipt").val();
	var password = jQ("#pwdipt").val();
	var autochecked = jQ.trim(""+jQ("#autocbx").attr("checked"));
	var p = /^[a-zA-Z0-9][a-zA-Z0-9_-]{5,19}$/;
	if(!isEmail(username)){
		var ccpos=username.lastIndexOf(".clip");
		var ccpos2=username.lastIndexOf(".test");
		if(ccpos>0&&ccpos==username.length-5){
			
		}else if(ccpos2>0&&ccpos2==username.length-5){
			
		}else{
			jalert("账号错误,请重新输入");
			return;
		}
	}
	if(!p.test(password)){
		jalert("密码错误,请重新输入");
		return;
	}
	jQ.ajax({
		type: "POST",
		url: "userAjax!login.action",
		data:{
			emailInput : username,
			pwdInput : password,
			activate : autochecked
		},
		success:function(data){
			if(data!=null){
				var dataObj=eval("("+data+")");
				if(dataObj.flag){
					if(dataObj.needactive){
						jQ("#ALERT").dialog({
							buttons:[{
								text : "确定",
								click: function() {
									resendActiveEmail();
								}
							},
							{
								text : "取消",
								click: function() {jQ(this).dialog("close");}
							}
							]
						});
						jQ("#ALERT p").empty().append("您的账号还未激活，是否需要再次发送激活邮件？");
						jQ("#ALERT").dialog('open');
					}else{
						var url=dataObj["visiturl"];
						if(url!=null&&url.length>0){
							jQ("#logininfozone").empty().append("登录成功，跳转到您之前的页面。");
							setTimeout(function(){
								window.location.href=url;
							},2000);
						}else
							window.location.href=jQ("#ctxVal").val()+"/myclip/calendar.html";
					}
				}else{
					jQ("#ALERT").dialog({
						buttons:[{
							text : "确定",
							click: function() {jQ(this).dialog("close");}
						}]
					});
					jQ("#ALERT p").empty().append(dataObj["result"]);
					jQ("#ALERT").dialog('open');
				}
			}
		}
	});
}